//
//  YHKPhotoCollectionViewCell.swift
//  xinlangweiBo2
//
//  Created by 杨和逵 on 16/3/7.
//  Copyright © 2016年 yanghekui. All rights reserved.
//

import UIKit

//协议
protocol YHKPhotoCollectionViewCellDelgate:NSObjectProtocol{
    
    //协议方法
    func deletePicture(index:Int)
    
    
}


class YHKPhotoCollectionViewCell: UICollectionViewCell {
    
    //协议属性
    weak var delgate:YHKPhotoCollectionViewCellDelgate?
    
    
    //定义一个属性，来接收下标
    var myIndexPath:NSIndexPath?
    
    
    //定义一个模型，来传递数据
    var iconImage: UIImage?{
        
        //赋值之后调用
        didSet{
            
            if iconImage == nil{
                //显示加号
                iconImageView.image = UIImage(named: "compose_pic_add")
                //隐藏按钮
                cancelButton.hidden = true
                
            }else{
            
                iconImageView.image = iconImage
                //显示按钮
                cancelButton.hidden = false
            
            }
        }
        
    }
    
    
    
    //第一部：init
   override init(frame: CGRect) {
        super.init(frame: frame)
    
    setUpView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //第二部：私有方法，添加控件和设置约束
    private func setUpView(){
        
       //添加控件
        contentView.addSubview(iconImageView)
        contentView.addSubview(cancelButton)
        
        //设置约束
        iconImageView.frame = bounds
        
        //按钮
        cancelButton.snp_makeConstraints { (make) -> Void in
            //shang  右
            make.right.equalTo(contentView.snp_right)
            make.top.equalTo(contentView.snp_top)
        }
        
        
        
        
    }
    
    //MARK:--懒加载
    //图片
    private lazy var iconImageView:UIImageView = UIImageView()
    
    //取消按钮
    private lazy var cancelButton:UIButton = {
    
    let btn = UIButton(type: .Custom)
    
        btn.setImage(UIImage(named: "compose_photo_close"), forState: .Normal)
        btn.setImage(UIImage(named: "compose_card_delete_highlighted"), forState: .Highlighted)
        btn.addTarget(self, action: "btnClick", forControlEvents: .TouchUpInside)
        
        //大小
        btn.sizeToFit()
        return btn
    
    }()
    

    
   @objc private func btnClick(){
        
        print("删除")
    //确定定义的cell进行删除
    if let index = myIndexPath?.item{
        
        delgate?.deletePicture(index)
        
        
    }
    
    }
    
    
    
    
}




